package com.hai.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * <p>
 * 菜单表
 * </p>
 *
 * @author NieChangan
 * @since 2022-05-21
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("tb_menu")
public class Menu extends Model<Menu> {

    /**
     * 菜单/按钮ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 上级菜单ID
     */
    @TableField("parent_id")
    private Long parentId;

    /**
     * 菜单/按钮名称
     */
    @TableField("menu_name")
    private String menuName;

    /**
     * 菜单URL
     */
    @TableField("url")
    private String url;

    /**
     * 权限标识
     */
    @TableField("perms")
    private String perms;

    /**
     * 图标
     */
    @TableField("icon")
    private String icon;

    /**
     * 类型 0菜单 1按钮
     */
    @TableField("type")
    private String type;

    /**
     * 排序
     */
    @TableField("order_num")
    private Long orderNum;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 修改时间
     */
    @TableField("modified_time")
    private LocalDateTime modifiedTime;

    /**
     * 0：不可用，1：可用
     */
    @TableField("available")
    private Integer available;

    /**
     * 0:不展开，1：展开
     */
    @TableField("open")
    private Integer open;

    /**
     * 子菜单
     */
    @TableField(exist = false) //表示不是数据库中的列
    private List<Menu> children;

    @Override
    public Serializable pkVal() {
        return this.id;
    }


    /**
     * 排序 根据order排序
     * @return
     */
    public static Comparator<Menu> order(){
        Comparator<Menu> comparator=(o1, o2) ->{
            if(o1.getOrderNum() != o2.getOrderNum()){
                return (int) (o1.getOrderNum() - o2.getOrderNum());
            }
            return 0;
        };
        return comparator;
    }
}
